package cn.com.jrj.base.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 抓取指定rul的内容<BR>
 * 依赖两个jar包: commons-httpclient-3.1.jar  和  commons-codec-1.3.jar
 */
public class HttpSnatch {

	private static final HttpSnatch instance = new HttpSnatch();
	
	private static Log log = LogFactory.getLog(HttpSnatch.class);

	public static HttpSnatch getInstance() {
		return instance;
	}

	public String GetWebContent(String url, String encode, int timeout) throws HttpException, IOException {
		log.debug("ZZZ: url = " + url);
		//MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
		SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
		HttpConnectionManagerParams params = new HttpConnectionManagerParams();
		params.setConnectionTimeout(timeout); // 设置连接超时时间
		params.setSoTimeout(timeout);
		connectionManager.setParams(params);// 设置连接参数
		HttpClient client = new HttpClient(connectionManager);
		GetMethod method = new GetMethod();
		
		int status = 0;
		try {
			method.setURI(new URI(url, false, encode));
			status = client.executeMethod(method);
		} catch (URIException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (java.net.SocketTimeoutException e) {
			e.printStackTrace();
		}
		
		if (status != 200) {
			return "";
		} else {
			//return method.getResponseBodyAsString();
			BufferedReader reader = null;
			StringBuffer sb = new StringBuffer();
			try {
				reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), encode));
				char[] buff = new char[4096];
				int read = 0;
				while ((read = reader.read(buff)) != -1) {
					sb.append(buff, 0, read);
				}
			} catch (Exception e) {
			} finally {
				if (reader != null) {
					reader.close();
				}
			}
			//connectionManager.shutdown();
			return sb.toString();
		}
	}

	public InputStream get(String url, String encode, int timeout) throws HttpException, IOException {
		SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
		HttpConnectionManagerParams params = new HttpConnectionManagerParams();
		params.setConnectionTimeout(timeout); // 设置连接超时时间
		params.setSoTimeout(timeout);
		connectionManager.setParams(params);// 设置连接参数
		HttpClient client = new HttpClient(connectionManager);
		GetMethod method = new GetMethod();
		try {
			method.setURI(new URI(url, false, encode));
		} catch (URIException e) {
			e.printStackTrace();
			return null;
		} catch (NullPointerException e) {
			e.printStackTrace();
			return null;
		}
		int status = client.executeMethod(method);
		if (status != 200) {
			return null;
		} else {
			return method.getResponseBodyAsStream();
		}
	}
}
